// components/benefits/benefits.js

import {Publish} from "../../models/other"
const publishModel = new Publish()
Component({
  lifetimes: {
    attached: function() {
      // 在组件实例进入页面节点树时执行
      this.init()
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    welfareList: [],
    socialBenefitList: [],
    socialBenefits: ''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onWelfare(e) {
      // 选择福利待遇
      const welfare = e.currentTarget.dataset.welfare
      const welfareList = this.data.socialBenefitList
      if (welfareList.indexOf(welfare) > -1) {
        welfareList.splice(welfareList.indexOf(welfare), 1)
      } else {
        welfareList.push(welfare)
      }
      this.setData({
        socialBenefits: welfareList.join(','),
        socialBenefitList: welfareList
      })
      this.triggerEvent('checked', this.data.socialBenefits)
    },
    getWelfareList () {
      // 获取福利待遇
      publishModel.getPublicTypes({
        tagKey: '福利待遇',
        type: 0
      }, res => {
        this.setData({
          welfareList: res
        })
      })
    },
    reset() {
      this.setData({
        socialBenefitList: [],
        socialBenefits: ''
      })
    },
    init() {
      this.getWelfareList()
    }
  }
})
